// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();


// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var date = event.date;
  var number = event.number;
  const sufferer= await db.collection("suffererTravel")
    .where(
      {
        t_no:number,
        t_date:date,
      }
    ).get();
  var sdata = sufferer.data;

  //交通数据只会匹配到1个或0个
  if(sdata.length == 0){
    await db.collection("myTravel").add({
      data:{
        _openid: wxContext.OPENID,
        date:date,
        number:number,
        dangerous:false,
        sufferer:-1
      }
    })
    return{dangerous:false}
  } else{
    await db.collection("myTravel").add({
      data: {
        _openid: wxContext.OPENID,
        date: date,
        number: number,
        dangerous: true,
        sufferer: sdata[0]._id
      }
    })
    return { dangerous: true }
  }
}